{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/user/miniconda3/envs/yjq-3.6/lib/python3.6/site-packages/tensorflow/python/compat/v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "non-resource variables are not supported in the long term\n"
     ]
    }
   ],
   "source": [
    "from dagmm import dagmm\n",
    "import sys\n",
    "sys.path.append('../../common/')\n",
    "from evaluator import *\n",
    "import warnings\n",
    "import os\n",
    "import numpy as np\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_datase(dataset):\n",
    "    folder = os.path.join(\"../../processed\", dataset)\n",
    "    if not os.path.exists(folder):\n",
    "        raise Exception(\"Processed Data not found.\")\n",
    "    loader = []\n",
    "    for file in [\"train\", \"test\", \"labels\"]:\n",
    "        loader.append(np.load(os.path.join(folder, f\"{file}.npy\")))\n",
    "    ## 准备数据\n",
    "    train_data = loader[0]\n",
    "    test_data = loader[1]\n",
    "    labels = loader[2][:,0].reshape(-1,1)\n",
    "    return train_data, test_data, labels"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SWaT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data, test_data, labels = load_datase(\"SWaT\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train data shape : (495000, 51)\n",
      "test data shape: (449919, 51)\n",
      "anomaly num : 54621\n",
      "anomaly ratio : 0.12140185233342002\n"
     ]
    }
   ],
   "source": [
    "print(f\"train data shape : {train_data.shape}\")\n",
    "print(f\"test data shape: {test_data.shape}\")\n",
    "print(f\"anomaly num : {np.count_nonzero(labels == 1)}\")\n",
    "ratio = np.count_nonzero(labels == 1)/labels.shape[0]\n",
    "print(f\"anomaly ratio : {ratio}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "dagmm_model = dagmm.DAGMM(epoch_size=5, contamination=ratio, comp_hiddens=[32,16,1])\n",
    "dagmm_model.fit(train_data)\n",
    "anomaly_score = dagmm_model.decision_function(test_data).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "without adjust\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'f1-score': 0.6810404070009258,\n",
       " 'precision': 0.8102947488671495,\n",
       " 'recall': 0.5873565111244107,\n",
       " 'TP': 32082.0,\n",
       " 'TN': 387787.0,\n",
       " 'FP': 7511.0,\n",
       " 'FN': 22539.0,\n",
       " 'threshold': 19.644011825561513}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(\"without adjust\")\n",
    "bf_search(labels, anomaly_score, verbose = False, is_adjust=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "point-adjust\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'f1-score': 0.7941507145430377,\n",
       " 'precision': 0.8617645796671027,\n",
       " 'recall': 0.7363834421309783,\n",
       " 'TP': 40222.0,\n",
       " 'TN': 388846.0,\n",
       " 'FP': 6452.0,\n",
       " 'FN': 14399.0,\n",
       " 'threshold': 20.647159934997394}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(\"point-adjust\")\n",
    "bf_search(labels, anomaly_score, verbose = False, is_adjust=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## WADI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data, test_data, labels = load_datase(\"WADI\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train data shape : (1209601, 123)\n",
      "test data shape: (172801, 123)\n",
      "anomaly num : 9860\n",
      "anomaly ratio : 0.0570598549776911\n"
     ]
    }
   ],
   "source": [
    "print(f\"train data shape : {train_data.shape}\")\n",
    "print(f\"test data shape: {test_data.shape}\")\n",
    "print(f\"anomaly num : {np.count_nonzero(labels == 1)}\")\n",
    "ratio = np.count_nonzero(labels == 1)/labels.shape[0]\n",
    "print(f\"anomaly ratio : {ratio}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "dagmm_model = dagmm.DAGMM(epoch_size=5, contamination=ratio, comp_hiddens=[32,16])\n",
    "dagmm_model.fit(train_data)\n",
    "anomaly_score = dagmm_model.decision_function(test_data).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "without adjust\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'f1-score': 0.12048361118667757,\n",
       " 'precision': 0.11008558473730023,\n",
       " 'recall': 0.1330628801895914,\n",
       " 'TP': 1312.0,\n",
       " 'TN': 152335.0,\n",
       " 'FP': 10606.0,\n",
       " 'FN': 8548.0,\n",
       " 'threshold': 38.919349670410156}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(\"without adjust\")\n",
    "bf_search(labels, anomaly_score, verbose = False, is_adjust=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "point-adjust\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'f1-score': 0.294644388582669,\n",
       " 'precision': 0.5691056894044549,\n",
       " 'recall': 0.19878296125884082,\n",
       " 'TP': 1960.0,\n",
       " 'TN': 161457.0,\n",
       " 'FP': 1484.0,\n",
       " 'FN': 7900.0,\n",
       " 'threshold': 38.925254821777344}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(\"point-adjust\")\n",
    "bf_search(labels, anomaly_score, verbose = False, is_adjust=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yjq-3.6",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
